﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;

namespace TS_XY_Train_Repaire.Main_Base
{
    public partial class Sys_Setting_Station_WorkPlace_Camera : DevExpress.XtraEditors.XtraForm
    {
        public string tree_id = "";
        public string tree_name = "";
        public Sys_Setting_Station_WorkPlace_Camera()
        {
            InitializeComponent();
        }

        private void Sys_Setting_Station_WorkPlace_Camera_Load(object sender, EventArgs e)
        {
            label1.Text = tree_name;
            InitStuffList();
         
        }

        private string GetFunclIst(TreeListNodes _Nodes, string _str)
        {
            string _Str = _str;
            foreach (TreeListNode node in _Nodes)
            {
                if  (node.Checked)
                {
                    if (node.GetValue("f_type").ToString() == "1")
                    {
                    _Str += node.GetValue("f_id").ToString() + ",";
                    }
           
                }
                if (node.HasChildren)
                {
       
                    _Str = GetFunclIst(node.Nodes, _Str);
                }
              

            }
            return _Str;
        }

        private void GetFunclIst1(TreeListNodes _Nodes, DataTable ds)
        {
           for (int i = 0; i < ds.Rows.Count; i++)
			{				
            foreach (TreeListNode node in _Nodes)
            {
                if (node.GetValue("f_id").ToString()==ds.Rows[i]["f_cid"].ToString())
                {
                   node.Checked=true;

                }
                if (node.HasChildren)
                {
                    GetFunclIst1(node.Nodes, ds);                 
                }
                }
            }
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ret = string.Empty;
            ret = GetFunclIst(treeListCanera.Nodes,"");
            string[] strarr = ret.Split(',');
            TS_Win_Class.MySqlCom("update TS_S_B_Station_WorkPlace_Camera  set f_del=1 , f_workerid= "+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + "  where f_swid='" + tree_id + "'" );
                foreach (var i in strarr)
                {
                    
                    if (i!="")
                    {
                     TS_Win_Class.MySqlCom("insert into TS_S_B_Station_WorkPlace_Camera(f_swid,f_cid,f_workerid) values("+tree_id+","+i+","+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")");
                    }
                }
                MessageBox.Show(ret+"摄像头完成关联！");
                this.DialogResult = DialogResult.OK; 
            }


        private void Sys_Setting_Station_WorkPlace_Camera_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void InitStuffList()
        {
          //  DataTable _dt = new DataTable();
            treeListCanera.Nodes.Clear();
            treeListCanera.Refresh();
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            ArrayList pList = new ArrayList();
            _ls = new LeverString_SysTreeMainList();
            _ls.f_id = "M-1";
            _ls.f_name = "摄像头目录";
            _ls.f_imgindex = 4;
            pList.Add(_ls);
            try
            {
                string strSQL = "select * from TS_S_B_Station_Camera where f_deep=0 and f_del=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader read = cmd.ExecuteReader();
                string _strUPID = "and (";
                int i = 0;
                while (read.Read())
                {
                    _ls = new LeverString_SysTreeMainList();
                    _ls.f_id = read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_parent = "M-1";
                    _ls.f_ck = false;
                    if (i == 0)
                    {
                        _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    else
                    {
                        _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    if (read["f_type"].ToString() == "0")
                    {
                        _ls.f_imgindex = 5;

                    }
                    else
                    {
                        _ls.f_imgindex = 9;
                        //_ls.f_ck.ColumnEdit = repositoryItemCheckEdit1;
                    }
                    pList.Add(_ls);
                    i++;
                }
                _strUPID += ")";
                if (_strUPID.Length > 7)
                {
                    read.Close();
                    strSQL = "select * from TS_S_B_Station_Camera where f_deep>0 and f_del=0 " + _strUPID + " order by f_deep,f_level,f_name asc";
                    cmd = new SqlCommand(strSQL, conn);
                    read = cmd.ExecuteReader();
                    string _UPID = "";
                    while (read.Read())
                    {
                        _ls = new LeverString_SysTreeMainList();
                        _ls.f_id = read["f_id"].ToString();
                        _ls.f_name = read["f_name"].ToString();
                        _ls.f_deep = read["f_deep"].ToString();
                        _ls.f_level = read["f_level"].ToString();
                        _ls.f_type = read["f_type"].ToString();
                        _UPID = read["f_level"].ToString();
                        _UPID = _UPID.Substring(0, _UPID.Length - 1);
                        _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                        _ls.f_parent = _UPID;
                        _ls.f_ck = false;
                        if (read["f_type"].ToString() == "0")
                        {
                            _ls.f_imgindex = 5;

                        }
                        else
                        {
                            _ls.f_imgindex = 9;
                        }
                        pList.Add(_ls);

                    }
                }
                read.Close();
                conn.Close();
                conn.Dispose();
            }
            catch
            {

            }

            treeListCanera.DataSource = pList;
            DataTable ds=TS_Win_Class.GetFilledDataTable( "select f_cid from TS_S_B_Station_WorkPlace_Camera where f_swid='"+tree_id+"' and f_del=0");
            GetFunclIst1(treeListCanera.Nodes,ds);           
        }


        private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
        {
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                node.Nodes[i].CheckState = check;
                SetCheckedChildNodes(node.Nodes[i], check);
            }
        }



     

        private void treeListCanera_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)  //选中菜单下所有子菜单及摄像头
        {
            SetCheckedChildNodes(e.Node, e.Node.CheckState);  
        }

        
 

     
      

    }
}